package com.todo.content.infrastructure.convert;


import com.todo.common.utils.TodoCommonUtils;
import com.todo.content.infrastructure.entity.domain.ContentDetailDO;
import com.todo.content.infrastructure.entity.po.ContentBaseRelationPO;
import com.todo.content.infrastructure.entity.po.ContentInfoPO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangwenxiang
 * @date 2024/4/6 21:54
 * @description
 */
public class ContentConvert {
    public static ContentInfoPO buildContentInfoPO(ContentDetailDO contentDetailDO) {
        if (contentDetailDO == null || contentDetailDO.getContentInfo() == null) {
            return null;
        }
        ContentInfoPO contentInfoPO = new ContentInfoPO();
        ContentDetailDO.ContentInfo contentInfo = contentDetailDO.getContentInfo();
        contentInfoPO.setTitle(contentInfo.getTitle());
        contentInfoPO.setId(contentDetailDO.getId());
        contentInfoPO.setUserId(contentDetailDO.getUserId());
        contentInfoPO.setIsPrivate(contentInfo.getIsPrivate());
        contentInfoPO.setLikeCount(contentInfo.getLikeCount());
        contentInfoPO.setMainImg(contentInfo.getMainImg());
        contentInfoPO.setSeeCount(contentInfo.getSeeCount());
        contentInfoPO.setSource(contentInfo.getSource());
        contentInfoPO.setStatus(contentInfo.getStatus());
        contentInfoPO.setAllowComment(contentInfo.getAllowComment());
        contentInfoPO.setClassifyCode(contentInfo.getClassifyCode());
        contentInfoPO.setCommentCount(contentInfo.getCommentCount());
        contentInfoPO.setCollectCount(contentInfo.getCollectCount());
        contentInfoPO.setContent(contentInfo.getContent());
        contentInfoPO.setExtra(contentInfo.getExtra());
        contentInfoPO.setIsDelete(0);
        contentInfoPO.setCreateTime(new Date());
        contentInfoPO.setUpdateTime(new Date());
        return contentInfoPO;
    }

    public static List<ContentBaseRelationPO> buildContentRelationList(ContentDetailDO contentDetailDO) {
        if (contentDetailDO == null || CollectionUtils.isEmpty(contentDetailDO.getContentRelationList())) {
            return null;
        }
        List<ContentBaseRelationPO> contentBaseRelationPOList = new ArrayList<>();
        contentDetailDO.getContentRelationList().forEach(item -> {
            ContentBaseRelationPO contentBaseRelationPO = new ContentBaseRelationPO();
            contentBaseRelationPO.setId(TodoCommonUtils.buildLongGuid());
            contentBaseRelationPO.setContentId(contentDetailDO.getId());
            contentBaseRelationPO.setRelationId(item.getRelationId());
            contentBaseRelationPO.setRelationName(item.getRelationName());
            contentBaseRelationPO.setRelationImg(item.getRelationImg());
            contentBaseRelationPO.setRelationType(item.getRelationType());
            contentBaseRelationPO.setSort(item.getSort());
            contentBaseRelationPO.setJumpUrl(item.getJumpUrl());
            contentBaseRelationPO.setIsOutside(item.getIsOutside());
            contentBaseRelationPO.setIsDelete(0);
            contentBaseRelationPO.setCreateTime(new Date());
            contentBaseRelationPO.setUpdateTime(new Date());
            contentBaseRelationPOList.add(contentBaseRelationPO);
        });

        return contentBaseRelationPOList;
    }

    public static ContentDetailDO convertToContentDetailDO(ContentInfoPO contentInfoPO, List<ContentBaseRelationPO> contentBaseRelationPOList) {
        if (contentInfoPO == null) {
            return null;
        }
        ContentDetailDO contentDetailDO = new ContentDetailDO();
        contentDetailDO.setId(contentInfoPO.getId());
        contentDetailDO.setUserId(contentInfoPO.getUserId());
        if (contentBaseRelationPOList != null) {
            List<ContentDetailDO.ContentRelation> contentRelationList = new ArrayList<>();
            contentBaseRelationPOList.forEach(item -> {
                ContentDetailDO.ContentRelation contentRelation = new ContentDetailDO.ContentRelation();
                contentRelation.setRelationId(item.getRelationId());
                contentRelation.setRelationType(item.getRelationType());
                contentRelation.setSort(item.getSort());
                contentRelation.setRelationName(item.getRelationName());
                contentRelation.setRelationImg(item.getRelationImg());
                contentRelation.setJumpUrl(item.getJumpUrl());
                contentRelation.setIsOutside(item.getIsOutside());
                contentRelationList.add(contentRelation);
            });
            contentDetailDO.setContentRelationList(contentRelationList);
        }
        ContentDetailDO.ContentInfo contentInfo = new ContentDetailDO.ContentInfo();
        contentInfo.setTitle(contentInfoPO.getTitle());
        contentInfo.setIsPrivate(contentInfoPO.getIsPrivate());
        contentInfo.setLikeCount(contentInfoPO.getLikeCount());
        contentInfo.setMainImg(contentInfoPO.getMainImg());
        contentInfo.setSeeCount(contentInfoPO.getSeeCount());
        contentInfo.setSource(contentInfoPO.getSource());
        contentInfo.setStatus(contentInfoPO.getStatus());
        contentInfo.setAllowComment(contentInfoPO.getAllowComment());
        contentInfo.setClassifyCode(contentInfoPO.getClassifyCode());
        contentInfo.setCommentCount(contentInfoPO.getCommentCount());
        contentInfo.setCollectCount(contentInfoPO.getCollectCount());
        contentInfo.setContent(contentInfoPO.getContent());
        contentInfo.setExtra(contentInfoPO.getExtra());
        contentDetailDO.setContentInfo(contentInfo);
        return contentDetailDO;
    }
}
